﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using super_scrable.Models.Exceptions;

namespace super_scrable.Models
{
    class WordConstructor : Word
    {
        List<Case> cases;

        public WordConstructor()
        {
            cases = new List<Case>();
        }

        public void AddCase(Case c) {
            cases.Add(c);
        }

        public int GetLetterValue(Case c)
        {
            int multiplicator = 1;

            if (c.getColor() == Case.BLEU_CLAIR) multiplicator =  2;
            if (c.getColor() == Case.BLEU_FONCE) multiplicator =  3;

            return c.getLetter().Point * multiplicator;
        }

        public int GetWordMultiplicator() {
            int highestMultiplicator = 1;
            int actualMultiplicator = 1;

            foreach (Case c in cases) {
                if (c.getColor() == Case.ROSE) actualMultiplicator = 2;
                if (c.getColor() == Case.ROUGE) actualMultiplicator = 3;

                if(highestMultiplicator < actualMultiplicator)
                    highestMultiplicator = actualMultiplicator;
                    
            }

            return highestMultiplicator;
        }

        public int GetWordValue()
        {
            int value = 0;

            foreach (Case c in cases)
                value += GetLetterValue(c);

            return value * GetWordMultiplicator();
        }

        public Boolean IsFilled(Case toCheck)
        {
            foreach (Case c in cases)
                if (c.CompareTo(toCheck) == 0)
                {
                    return true;
                }
            return false;
        }

        public Word GetWord() {
            Word word = new Word();

            foreach (Case c in cases)
                word.AddLetter(c.getLetter());

            return word;
        }

        public void SwitchCases(Case initial, Case target)
        {
            char tmpLetter = initial.Letter;

            foreach (Case c1 in cases)
                if (c1.CompareTo(initial) == 0)
                    foreach (Case c2 in cases)
                        if (c2.CompareTo(target) == 0)
                        {
                            initial.Letter = target.Letter;
                            target.Letter = tmpLetter;
                        }
        }

        /// <summary>
        ///     Return true if the targeted Case is next to the previous one and the word Direction is the same than previously.
        ///     Throws InvalidCaseException or InvalidDirectionException in case of invalide placment case input.
        /// </summary>
        /// <param name="c">The targeted Case</param>
        /// <returns></returns>
        public Boolean CanBePlaced(Case c)
        {
            if (cases.Count == 0)
                return true;

            if (!cases[cases.Count - 1].IsNextTo(c))
                return false;

            if (!(cases[0].GetDirection(c) == cases[cases.Count - 1].GetDirection(c)))
                return false;

            return true;
        }
    }
}
